﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;

namespace Prototype
{
    public class RoomType
    {
        private int capacity;

        public int Capacity
        {
            get { return capacity; }
            set { capacity = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private decimal prise;

        public decimal Prise
        {
            get { return prise; }
            set { prise = value; }
        }
    
        public void Insert(string name, int capacity, decimal prize)
        {
            string str = ConfigurationManager.ConnectionStrings[0].ToString();
            string commandString =
                " insert into RoomType([Name],Capacity,Prise) Values (@name,@capacity,@price)";

            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            try
            {
                SqlParameter pName = new SqlParameter("@name", SqlDbType.NChar);
                SqlParameter pCapacity = new SqlParameter("@capacity", SqlDbType.Int);
                SqlParameter pPrice = new SqlParameter("@price", SqlDbType.Money);
                pName.Value =name;
                pCapacity.Value = capacity;
                pPrice.Value = prise;

                SqlCommand commandInsert = new SqlCommand(commandString);
                commandInsert.Parameters.Add(pName);
                commandInsert.Parameters.Add(pCapacity);
                commandInsert.Parameters.Add(pPrice);
                commandInsert.ExecuteNonQuery();
            }
            finally { conn.Close(); }
        }

        public void Change(string name, int capacity, decimal prize)
        {
         throw new System.NotImplementedException();           
        }

        public static RoomType[] Get()
        {
        string str = ConfigurationManager.ConnectionStrings[0].ToString();
            string commandString =
                " select * from RoomType";
            List<RoomType> result = new List<RoomType>();
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            try
            {
                SqlCommand commandSelect = new SqlCommand(commandString);
               SqlDataReader reader= commandSelect.ExecuteReader(); ;
                using (reader)
                {
                    while (reader.Read())
                    {
                        RoomType type = new RoomType();
                        type.Name = (string)reader["Name"];
                        type.Prise = (decimal)reader["Prise"];
                        type.Capacity = (int)reader["Capacity"];
                        result.Add(type);
                    }
                }
                return result.ToArray();

                

            }
            finally { conn.Close(); }
            
        }
    }
}
